package ks.app.smartDic;

import java.net.URLEncoder;

public class NaverURLManager {
	private static String sortOfDictionary;
	private static String query;

	private static final int MODE_DEFINITION = 1;
	private static final int MODE_WORD = 2;
	private static final int MODE_ARTICLE = 3;
	private static final int MODE_EXAMPLE = 4;
	private static final int MODE_SYNONYM = 5;

	static String ENG_TO_KOREAN = "ENG_TO_KOREAN";
	static String KOREAN_TO_ENG = "KOREAN_TO_ENG";
	static String KOREAN_TO_KOREAN = "KOREAN_TO_KOREAN";
	static String ENG_TO_ENG = "ENG_TO_ENG";

	NaverURLManager() {

	}

	public static String getUrl(int mode) {
		String RequestedUrl = null;

		if (mode == MODE_DEFINITION) {
			if (getSortOfDictionary().compareTo(ENG_TO_KOREAN) == 0
					|| getSortOfDictionary().compareTo(KOREAN_TO_ENG) == 0) {

				RequestedUrl = "http://m.endic.naver.com/search.nhn?query="
						+ getQuery();

			} else if (getSortOfDictionary().compareTo(KOREAN_TO_KOREAN) == 0) {

				RequestedUrl = "http://krdic.naver.com/search.nhn?kind=all&query="
						+ getQuery();

			}

			else if (getSortOfDictionary().compareTo(ENG_TO_ENG) == 0) {

				RequestedUrl = "http://endic.naver.com/search.nhn?isOnlyViewEE=Y&query="
						+ getQuery();

			}
		} else if (mode == MODE_WORD) {
			if (getSortOfDictionary().compareTo(ENG_TO_KOREAN) == 0
					|| getSortOfDictionary().compareTo(KOREAN_TO_ENG) == 0) {

				RequestedUrl = "http://m.endic.naver.com/search.nhn?searchOption=entryIdiom&query="
						+ getQuery();

			} else if (getSortOfDictionary().compareTo(KOREAN_TO_KOREAN) == 0) {

				RequestedUrl = "http://krdic.naver.com/search.nhn?kind=all&query="
						+ getQuery();

			} else if (getSortOfDictionary().compareTo(ENG_TO_ENG) == 0) {

				RequestedUrl = "http://endic.naver.com/search.nhn?isOnlyViewEE=Y&query="
						+ getQuery();

			}

		} else if (mode == MODE_ARTICLE) {
			if (getSortOfDictionary().compareTo(ENG_TO_KOREAN) == 0
					|| getSortOfDictionary().compareTo(KOREAN_TO_ENG) == 0) {

				RequestedUrl = "http://m.endic.naver.com/search.nhn?searchOption=mean&query="
						+ getQuery();

			} else if (getSortOfDictionary().compareTo(KOREAN_TO_KOREAN) == 0) {

				RequestedUrl = "http://krdic.naver.com/search.nhn?kind=all&query="
						+ getQuery();

			}
		} else if (mode == MODE_EXAMPLE) {
			if (getSortOfDictionary().compareTo(ENG_TO_KOREAN) == 0
					|| getSortOfDictionary().compareTo(KOREAN_TO_ENG) == 0) {

				RequestedUrl = "http://m.endic.naver.com/search.nhn?searchOption=example&query="
						+ getQuery();
			} else if (getSortOfDictionary().compareTo(KOREAN_TO_KOREAN) == 0) {

				RequestedUrl = "http://krdic.naver.com/search.nhn?kind=all&query="
						+ getQuery();

			}
		} else if (mode == MODE_SYNONYM) {
			if (getSortOfDictionary().compareTo(ENG_TO_KOREAN) == 0) {

				RequestedUrl = "http://m.endic.naver.com/search.nhn?searchOption=thesaurus&query="
						+ getQuery();
			} else if (getSortOfDictionary().compareTo(KOREAN_TO_KOREAN) == 0) {

				RequestedUrl = "http://krdic.naver.com/search.nhn?kind=all&query="
						+ getQuery();

			}
		}

		return RequestedUrl;

	}

	public static void setSortOfDictionary(String sortOfDictionary) {
		NaverURLManager.sortOfDictionary = sortOfDictionary;
	}

	public static String getSortOfDictionary() {
		return sortOfDictionary;
	}

	public static void setQuery(String query) {
		NaverURLManager.query = query;
	}

	public static String getQuery() {

		String encodedStr = null;
		try {
			encodedStr = URLEncoder.encode(query, "UTF-8");
		} catch (Exception e) {
			ksLog.d("e", e.getMessage());
		}

		return encodedStr;
	}
}
